//
//  ConfigManager.m
//  dongbo
//
//  Created by 葛殿宏 on 16/2/22.
//  Copyright © 2016年 arenacloud. All rights reserved.
//

#import "ConfigManager.h"

#define SANDBOX_CONFIG_PATH                                  @"config"

@implementation ConfigManager

+ (instancetype)shareConfigManager {
    static ConfigManager *shared_manager = nil;
    static dispatch_once_t pred;
    dispatch_once(&pred, ^{
        shared_manager = [[self alloc] init];
    });
    return shared_manager;
}

//取内网服务器地址
- (NSString *)getDebugAppUrl{
    return [[self getConfigDic] objectForKey:@"debug_url"];
}

//取外网服务器地址
- (NSString *)getReleaseAppUrl{
    return [[self getConfigDic] objectForKey:@"release_url"];
}

//取内网支付地址
- (NSString *)getDebugPayUrl{
    return [[self getConfigDic] objectForKey:@"debug_pay_url"];
}

//取外网支付地址
- (NSString *)getReleasePayUrl{
    return [[self getConfigDic] objectForKey:@"release_pay_url"];
}

//取内网socket服务器地址
- (NSString *)getDebugSocketUrl{
    return [[self getConfigDic] objectForKey:@"debug_socket"];
}

//取外网socket服务器地址
- (NSString *)getReleaseSocketUrl{
    return [[self getConfigDic] objectForKey:@"release_socket"];
}

//取app版本
- (NSString *)getAppVersion{
    return [[self getConfigDic] objectForKey:@"version"];
}

-(NSDictionary *)getConfigDic{
    NSString *path = [self configPath];
    if (!path) return nil;
    
    NSString *dic = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    if (dic) {
        NSData *jsonData = [dic dataUsingEncoding:NSUTF8StringEncoding];
        if ( [jsonData length] > 0  ) {
            NSError *error = nil;
            if ([jsonData length] == 0) {
                return   nil;
            } else {
                return  [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
            }
        }
    }
    return nil;
}

- (NSString *)configPath{
    return [[NSBundle mainBundle]pathForResource:SANDBOX_CONFIG_PATH ofType:@"json"];
}

@end
